<script setup lang="ts">
import {onMounted, ref} from "vue";
import {ComponentSize, ElMessage, ElMessageBox} from 'element-plus'
import {
  useConfirmStockTakeAPI,
  useGetAllCountAPI,
  useGetAllMaterialAPI,
  useGetByMultifunctionAPI,
  useGetByTypeAPI,
  useGetCountByMultifunctionNameAPI,
  useGetCountsByTypeNameAPI,
  useModifyMaterialByStockTakeNumberAPI
} from "@/apis/material.js";
import {useGetMaterialTypeAPI} from "@/apis/materialType.js";


//
// const dataPublicStore = useDataPublicStore();
// const materialStore=useMaterialStore();
// const materialTypeStore=useMaterialTypeStore();


// 定义搜索标志
const getAllListFlag=ref(true);
const getByTypeListFlag=ref(false);
const getByNameListFlag=ref(false);


const materialList=ref<any>([]);
const allMaterialCount=ref(0);
const materialTypeList=ref<any>([]);

// 获取所有的设备，形成列表，同时获取所有数据函数，用于分页
onMounted( async ()=>{
  // 获取全部材料列表
  // await materialStore.getAllMaterialByPageStore(current.value,pageSize.value);
  useGetAllMaterialAPI(current.value,pageSize.value).then((res:any)=>{
    materialList.value=res.data;
  })
  // 获取全部材料类别列表
  // await materialTypeStore.getFullMaterialType(1,100000);
  const res=await  useGetMaterialTypeAPI(1,100000);
  materialTypeList.value=res.data;



  // 获取全部材料数据列表条数
  // await materialStore.getAllCountStore();
  await useGetAllCountAPI().then((res:any)=>{
    allMaterialCount.value=res.data;
  })

})


// 分页组件的每页条数值改变
const handleSizeChange=async (val:any)=>{
  // 这种是没有其它查询条件，纯粹是查询全部数据
  if (getAllListFlag.value===true){
    // await materialStore.getAllMaterialByPageStore(current.value,val);
    const res=await useGetAllMaterialAPI(current.value,val);
    materialList.value=res.data;
  }
  // 这种是根据材料类型查询数据
  else if (getByTypeListFlag.value===true){
    typeFullName.value=typeValueList.value[0]+':'+typeValueList.value[1];
    // await materialStore.getMaterialByTypeStore(typeFullName.value,1,current.value,val);
    const res=await useGetByTypeAPI(typeFullName.value,1,current.value,val);
    materialList.value=res.data;
    allMaterialCount.value=(await useGetCountsByTypeNameAPI(typeFullName.value)).data;
  }
  // 根据多功能名称查询到的数据
  else if (getByNameListFlag.value===true){
    // await materialStore.getByMultifunctionStore(multifunctionName.value,current.value,val)
    const res=await useGetByMultifunctionAPI(multifunctionName.value,current.value,val);
    materialList.value=res.data;
    const resCount= await useGetCountByMultifunctionNameAPI(multifunctionName.value)
    allMaterialCount.value=resCount.data;
  }
}


// 分页组件的第几页数值改变
const handleCurrentChange=async (val:any)=>{
  // 没有查询条件，查询所有数据
  if (getAllListFlag.value===true){
    const res=await useGetAllMaterialAPI(val,pageSize.value);
    materialList.value=res.data;
    const resCount=await useGetAllCountAPI();
    allMaterialCount.value=resCount.data;
  }
  // 根据类型查询到的数据
  else if (getByTypeListFlag.value===true){
    typeFullName.value=typeValueList.value[0]+':'+typeValueList.value[1];
    const res=await useGetByTypeAPI(typeFullName.value,1,val,pageSize.value);
    materialList.value=res.data;
    allMaterialCount.value=(await useGetCountsByTypeNameAPI(typeFullName.value)).data;
  }
  // 根据多功能名称查询到的数据
  else if (getByNameListFlag.value===true){
    const res=await useGetByMultifunctionAPI(multifunctionName.value,val,pageSize.value);
    materialList.value=res.data;
    const resCount=await useGetCountByMultifunctionNameAPI(multifunctionName.value);
    allMaterialCount.value=resCount.data;
  }
}

// 根据全设备类型名称查询
const typeValueList=ref([])
const typeFullName=ref('')


const getMaterialByTypeName=async ()=>{
  getAllListFlag.value=false;
  getByTypeListFlag.value=true;
  getByNameListFlag.value=false;

  if (typeValueList.value===undefined){
    // 获取全部材料列表,这个函数已经包括了获取counts的函数
    // await materialStore.getAllMaterialByPageStore(current.value,pageSize.value);
    const res=await useGetAllMaterialAPI(current.value,pageSize.value);
    materialList.value=res.data;
    const resCount=await useGetAllCountAPI();
    allMaterialCount.value=resCount.data;
  }
  else {
    typeFullName.value=typeValueList.value[0]+':'+typeValueList.value[1];
    // 下面这个函数，在store里面已经包括了一个获取counts的函数
    //  allMaterialCount.value=(await useGetCountsByTypeNameAPI(typeName)).data
    const res=await useGetByTypeAPI (typeFullName.value,1,current.value,pageSize.value);
    materialList.value=res.data;
    allMaterialCount.value=(await useGetCountsByTypeNameAPI(typeFullName.value)).data;


  }
}

// todo 多功能查询字符串
const multifunctionName=ref('')
// todo 这个需要在后端重新修改做一个新的接口
const getListByMultifunctionName=async ()=>{
  getAllListFlag.value=false;
  getByTypeListFlag.value=false;
  getByNameListFlag.value=true;
  // await materialStore.getByMultifunctionStore(multifunctionName.value,current.value,pageSize.value);
  const res=await useGetByMultifunctionAPI (multifunctionName.value,current.value,pageSize.value);
  materialList.value=res.data;
  const resCount= await useGetCountByMultifunctionNameAPI(multifunctionName.value);
  allMaterialCount.value=resCount.data;
}

// 分页组件
const size = ref<ComponentSize>('small');
const background = ref(true)
const disabled = ref(false)
const current=ref(1);
const pageSize=ref(15);

const reloadAllEquipmentList=async ()=>{
  // 获取全部列表
  const resMaterial=await useGetAllMaterialAPI(current.value,pageSize.value);
  materialList.value=resMaterial.data;
  const resCount=await useGetAllCountAPI();
  allMaterialCount.value=resCount.data;
  // 获取全部设备类型
  // await materialTypeStore.getFullMaterialType(current.value, pageSize.value);
  const resMaterialType=await  useGetMaterialTypeAPI(1, 10000);
  materialTypeList.value=resMaterialType.data;
}

const stockTake=async (val:any)=>{
  if(val.number===val.stockTakeNumber){
    ElMessage({
      message: '库存数量和盘点数量一致，无需修改',
      type: 'warning',
    })
    return;
  }

  try {
    await ElMessageBox.confirm(
        '确定要修改该材料的库存数量吗？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
    ).then(async () => {
      await useModifyMaterialByStockTakeNumberAPI(val);
    }).catch(() => {
      window.location.reload();
    })


    ElMessage.success('库存数量修改成功');

  } catch (error) {
    ElMessage.error('取消修改');
  }
}

const stockTakeSubmit=async ()=>{
  try {
    await ElMessageBox.confirm(
        '确定要提交盘点吗？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
    ).then(async () => {
      await useConfirmStockTakeAPI();
    })
    ElMessage.success('succeed');
  }catch (error) {
    ElMessage.error('fail');
  }



}



</script>

<template>

  <!-- 面包屑 -->
  <div class="bread-container">
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: `/` }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>{{"材料盘点管理"}}</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <div>
    <div class="equipmentController">
      <div class="m-4">
        <div class="m-4">
          <el-cascader
              placeholder="请选择材料类型"
              :options="materialTypeList"
              filterable
              v-model="typeValueList"
              @change="getMaterialByTypeName"
              clearable
              style="margin-right: 20px"
          />
          <el-input v-model="multifunctionName" clearable style="width: 450px" placeholder="请输入材料名称/品牌/创建人/修改人/编码/类型/货架号" @clear="reloadAllEquipmentList"  />&nbsp;
          <el-button type="info" size="small" @click="getListByMultifunctionName">名称查询</el-button>
          <el-button type="primary"  size="small" @click="stockTakeSubmit">确认盘点</el-button>

        </div>
      </div>
    </div>
  </div>
  <!--  分页显示设备数据-->
  <div class="demo-pagination-block" style="float: right">
    <el-pagination
        v-model:current-page="current"
        v-model:page-size="pageSize"
        :page-sizes="[5, 15, 30, 45]"
        :size="size"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="allMaterialCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>

  <!-- 对表格中的字段排序，在表格属性中添加:default-sort,后面参数是需要排序的字段，
       order:后面是排序要求,这里是descending,然后在需要排序的列元素中添加sortable属性-->
  <div >
    <el-table
        :data="materialList"
        style="width: 100%"
        stripe
        border
        max-height="800"
        :default-sort="{prop:'updateTime',order:'descending',}"
    >
      <el-table-column align="center" prop="materialName" fixed label="材料名称" width="280" />

      <el-table-column align="center" prop="brand" label="品牌" width="120"/> >

      <el-table-column align="center" prop="model" label="型号" width="130" />

      <el-table-column align="center" prop="pieces" label="单位" width="80" />

      <el-table-column align="center" prop="number" label="数量" width="70"/>

      <el-table-column align="center" prop="typeName" label="材料类型" width="120"/>

      <el-table-column align="center" prop="layout" label="货架号" style="font-size: large;font-weight: bold" width="150"/>

      <el-table-column  align="center" label="盘点数量" width="250" fixed="right">
        <template #default="scope">
          <el-input-number v-model="scope.row.stockTakeNumber" size="small" min="0" style="margin-left: 10px;margin-right: 10px"/>
          <el-button size="small" type="warning" plain  @click="stockTake(scope.row)">确定</el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>

</template>

<style scoped lang="scss">
.block {
  margin-right: 260px;
}

.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }

  .sub-list {
    margin-top: 20px;
    background-color: #fff;

    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;

      li {
        width: 168px;
        height: 160px;


        a {
          text-align: center;
          display: block;
          font-size: 16px;

          img {
            width: 100px;
            height: 100px;
          }

          p {
            line-height: 40px;
          }

          &:hover {
            color: $xtxColor;
          }
        }
      }
    }
  }

  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;

    .head {
      // .xtx-more {
      //   position: absolute;
      //   top: 20px;
      //   right: 20px;
      // }

      // .tag {
      //   text-align: center;
      //   color: #999;
      //   font-size: 20px;
      //   position: relative;
      //   top: -20px;
      // }
    }

    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
  }
}
.home-banner {
  width: 1240px;
  height: 500px;
  margin: 0 auto;

  img {
    width: 100%;
    height: 500px;
  }
}

.table_contain {
  margin-left: 25px;
  padding-left: 35px;
}

.bread-container {
  padding: 25px 10px;
  margin-left: 10px;
}
.equipmentController{
  padding-left: 80px;
}
.equipmentParentType{
  appearance: none;
  text-align: center;
  height: 30px;
  width: 150px;
  //border-radius: 15px;
  padding: 0 8px;
  outline: 0;
  letter-spacing: 1px;
  color: #1a1616;
  font-weight: 600;
  background: rgba(45,45,45,.10);
  border: 1px solid rgba(255,255,255,.15);
  box-shadow: 0 2px 3px 0 rgba(0,0,0,.1) inset;
  text-shadow: 0 1px 2px rgba(0,0,0,.1);
}
</style>